﻿Public Class Status_osDAO

    Dim objAcessoDados As AcessoDados = New AcessoDados(Config.StringConexao)

    Public Function ListaTodos() As List(Of Status_Os)
        Dim registros As DataSet = New DataSet
        registros = objAcessoDados.ExecutaSqlRetorno("select * from tb_status_os")
        Return CriarListaStatus(registros)
    End Function

    Public Function CriarListaStatus(ByVal Registros As DataSet) As List(Of Status_Os)
        Dim lista As New List(Of Status_Os)
        Dim objStatus As Status_Os
        For Each dr As DataRow In Registros.Tables(0).Rows
            objStatus = CriarStatus(dr)
            lista.Add(objStatus)
        Next
        Return lista
    End Function
    Public Function CriarStatus(ByVal row As DataRow) As Status_Os
        Dim objStatus As New Status_Os

        objStatus.CodStat = CInt(row.Item("cod_stat"))
        objStatus.DescStat = CStr(row.Item("desc_stat"))
      
        Return objStatus
    End Function

    Public Function Inserir(ByVal iStatus As Status_Os) As Integer
        Dim registros As Integer
        Dim sql As New StringBuilder
        sql.Append("insert into tb_status_os (")
        sql.Append(" desc_stat ")
        sql.Append(") values (")
        sql.Append("'" & iStatus.DescStat.ToString & "')")
        registros = objAcessoDados.ExecutaSql(sql.ToString)
        Return registros
    End Function

    Public Function selectStatusId(ByVal cod_status_os As Integer) As Status_Os
        Dim objStatus As Status_Os
        Dim registro As DataSet = New DataSet

        Dim sql As New StringBuilder
        Dim strWhere As String = " Where "
        sql.Append("select * from tb_status_os ")
        sql.Append(strWhere & "cod_stat = " & cod_status_os)
        registro = objAcessoDados.ExecutaSqlRetorno(sql.ToString)
        Try
            Dim objStatusData As DataRow = registro.Tables(0).Rows(0)
            objStatus = CriarStatus(objStatusData)
            Return objStatus
        Catch ex As Exception
            Return Nothing
        End Try
        Return Nothing
    End Function

    Public Function selectStatusDesc(ByVal status As Status_Os) As Status_Os
        Dim objStatus As Status_Os
        Dim registro As DataSet = New DataSet

        Dim sql As New StringBuilder
        Dim strWhere As String = " Where "
        sql.Append("select * from tb_status_os ")
        sql.Append(strWhere & "desc_stat = '" & status.DescStat & "'")
        registro = objAcessoDados.ExecutaSqlRetorno(sql.ToString)
        Try
            Dim objStatusData As DataRow = registro.Tables(0).Rows(0)
            objStatus = CriarStatus(objStatusData)
            Return objStatus
        Catch ex As Exception
            Return Nothing
        End Try
        Return Nothing
    End Function
  
End Class
